﻿using DevExpress.XtraEditors;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.Setup;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmParameterComputerSetup : FrmSetupWithSelect
    {
        public FrmParameterComputerSetup()
        {
            InitializeComponent();
            base.nbiFresh.Caption = "筛选";
        }
        protected override bool BeforeSave()
        {
            if ((this.gvParameter.DataSource as DataView).Table.Select("PARAMETER_NAME IS NULL OR PARAMETER_VALUE IS NULL OR PARAMETER_TYPE IS NULL OR PARAMETER_NAME_CHN IS NULL").Length > 0)
            {
                XtraMessageBox.Show("请填写完整信息!");
                return false;
            }
            return base.BeforeSave();
        }

        protected void DispAllInfo()
        {
            SystemInfo.DbData.Retrieve("SELECT COMPUTER_CODE ,PARAMETER_NAME, PARAMETER_NAME_CHN, PARAMETER_VALUE, PARAMETER_TYPE ,DESCRIBE FROM MXHLY_SYS_PARAMETER_COMPUTER ORDER BY PARAMETER_NAME ASC", ref this.dtSetup);
            base.dtSetup.Columns["PARAMETER_TYPE"].DefaultValue = "C";
            base.dtSetup.Columns["COMPUTER_CODE"].DefaultValue = base.mainCode;
        }

        protected void DispInfo()
        {
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("code", base.mainCode.ToString()) });
            base.RetrieveData(parm);
            base.dtSetup.Columns["PARAMETER_TYPE"].DefaultValue = "C";
            base.dtSetup.Columns["COMPUTER_CODE"].DefaultValue = base.mainCode;
        }

        private void FrmSysConfigSetup_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("TYPE_CODE", typeof(string));
            table.Columns.Add("TYPE_NAME", typeof(string));
            table.Rows.Add(new object[] { "C", "字符" });
            table.Rows.Add(new object[] { "N", "数字" });
            table.Rows.Add(new object[] { "D", "日期" });
            this.riParameterType.DataSource = table;
            this.riParameterType.DisplayMember = "TYPE_NAME";
            this.riParameterType.ValueMember = "TYPE_CODE";
            this.dtComputer = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT COMPUTER_CODE ,COMPUTER_NAME FROM MXHLY_SYS_COMPUTER ORDER BY COMPUTER_CODE", ref this.dtComputer);
            this.riParameterComputerCode.DisplayMember = "COMPUTER_NAME";
            this.riParameterComputerCode.ValueMember = "COMPUTER_CODE";
            this.riParameterComputerCode.DataSource = this.dtComputer;
            this.gvParameter.Columns["PARAMETER_NAME_CHN"].Tag = "ON";
            this.gvParameter.Columns["DESCRIBE"].Tag = "ON";
            base.gvSetup = this.gvParameter;
            string selsql = "SELECT COMPUTER_CODE ,PARAMETER_NAME, PARAMETER_NAME_CHN, PARAMETER_VALUE, PARAMETER_TYPE ,DESCRIBE FROM MXHLY_SYS_PARAMETER_COMPUTER WHERE COMPUTER_CODE = :code ORDER BY PARAMETER_NAME ASC ";
            base.setSql(selsql, "MXHLY_SYS_PARAMETER_COMPUTER", new string[] { "COMPUTER_CODE", "PARAMETER_NAME" }, null, "", "");
            base.ZoomWin();
            base.mainCode = SystemInfo.ComputerID;
            this.DispInfo();
            base.selectFresh();
            base.nbiFresh.Visible = SystemInfo.OperIsAdmin;
            this.gcParameterComputerCode.OptionsColumn.AllowEdit = SystemInfo.OperIsAdmin;
        }

        protected override bool selectFresh()
        {
            base.selectFresh();
            DataTable dt = this.dtComputer.Copy();
            DataRow row = dt.NewRow();
            row["COMPUTER_CODE"] = "*";
            row["COMPUTER_NAME"] = "全部";
            dt.Rows.InsertAt(row, 0);
            FrmSelectFromGrid grid = new FrmSelectFromGrid(dt, new string[] { "编号", "机器名" }, new int[] { 40, 50 }, "机器选择");
            if (grid.ShowDialog(this) != DialogResult.Cancel)
            {
                int choiceRow = grid.ChoiceRow;
                this.Text = "机器参数设置 --- " + dt.Rows[choiceRow]["COMPUTER_NAME"].ToString();
                grid.Dispose();
                if (choiceRow == 0)
                {
                    base.mainCode = SystemInfo.ComputerID;
                    this.DispAllInfo();
                }
                else
                {
                    base.mainCode = dt.Rows[choiceRow]["COMPUTER_CODE"];
                    this.DispInfo();
                }
                return true;
            }
            base.mainCode = new object();
            return false;
        }
    }
}
